package com.ithema.comfig;

import com.ithema.controller.interception.ProjectInterception;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@ComponentScan({"com.ithema.controller", "com.ithema.comfig"})//com.ithema.controller 面对拦截器interception包下也是能扫描的
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
    //拦截器的简化开发    直接在springmvcconfig配置类中实现拦截器,实现 WebMvcConfigurer      注意：侵入式较强，与springconfig强绑定



    @Autowired
    private ProjectInterception projectInterception;//这里设置拦截器对象
    //动态  可变
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //addPathPatterns(可变参数)方法，可以写多个，   相应/books指拦截rest风格下对应相应方法
        registry.addInterceptor(projectInterception).addPathPatterns("/books","/books/*");
    }

    //静态  固定   该方法下的资源路径不被springmvc自动拦截，                可以对静态资源放行   html css等
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");//将pages目录下的请求指向/pages/下
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
    }
}
